<?php
/**
* 
*/
class ReserveModel extends MY_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_user_appoint';
	}


	//获取列表数据
	public function get_list_data($term = array(),$page=1,$size=10)
	{

		$where = " where 1 ";
		
		//查询
		if (!empty($term['sdate'])) {
			$time = $term['sdate'];
			$where .= " AND ap.sdate = '$time'";
		}

		//场馆
		if (!empty($term['venue_id']) && $term['venue_id'] > 0) {
			$where .= " AND ap.shop_id = '$term[venue_id]'";
		}

		//教练
		if (!empty($term['coach_id']) && $term['coach_id'] > 0) {
			$where .= " AND ap.mid = '$term[coach_id]'";
		}
		
		//状态
		if (isset($term['status']) && $term['status'] != '-1') {
			$where .= " AND ap.status = '$term[status]'";
		}

		//搜索会员
		if (!empty($term['keyword'])) {

			$query = $this->db->query("SELECT id FROM nmp_user WHERE (nickname LIKE '%".$term['keyword']."%' OR mobile LIKE '%".$term['keyword']."%')");
			$user = $query->result_array();

			$str = '';
			foreach ($user as $key => $value) {
				$str .= $value['id'].',';
			}
			$str = !empty($str) ? rtrim($str,',') : 0;
			$where .= " AND ap.user_id in($str)";
		}

		$query = $this->db->query("SELECT ap.*, co.nickname as coach_name, sh.name as shop_name, u.nickname as user_name, u.headimg, u.mobile, cou.name as course_name, cou.type_id, card.price FROM 
				".$this->table." AS ap LEFT JOIN 
				nmp_course AS cou ON ap.cid = cou.id LEFT JOIN 				
				nmp_coach AS co ON ap.mid = co.id LEFT JOIN 
				nmp_shop AS sh ON ap.shop_id = sh.id LEFT JOIN 
				nmp_user AS u ON ap.user_id = u.id LEFT JOIN 
				nmp_user_card AS card ON ap.card_id=card.id
				
				".$where." ORDER BY ap.id DESC LIMIT ".($page-1)*$size.",".$size);
		
		
		$data = $query->result_array();

		foreach ($data as $key => $value){			
			//0私教 1团教			
			$data[$key]['type_name'] = '';			
			if(isset($value['type_id'])){
				$type = array("私教","团教");
				$type_id = $value['type_id'];
				$data[$key]['type_name'] = $type[$type_id];
			}			
			
			//0未签到,1已签到,2已取消预约
			$status = array("未签到","已签到","已取消预约");
			$status_id = $value['status'];
			$data[$key]['status_name'] = $status[$status_id];
			
			$data[$key]['course_name'] = $value['course_name']?$value['course_name']:'';
		}
		
		
		
		$query = $this->db->query("select COUNT(*)  as num from ".$this->table." AS ap ".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages);
	}
	
	function get_info($id){
		$sql = "select * from ".$this->table." where  id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}

	//获取预约用户
	public function appoint_user($coach_id,$time,$where)
	{
		$query = $this->db->query("SELECT ap.*, us.nickname, us.mobile, us.headimg, ca.card_name, ca.price FROM ".$this->table." AS ap 
				LEFT JOIN nmp_user AS us ON ap.user_id = us.id 
				JOIN nmp_user_card AS uc ON ap.card_id = uc.id 
				JOIN nmp_course_card AS ca ON uc.card_id = ca.id
				".$where." and ap.mid = '$coach_id' ORDER BY ap.addtime DESC");

		$data = $query->result_array();

		foreach ($data as $key => $value) 
		{
			$data[$key]['status_name'] = $value['status'] == 1 ? '<font style="font-weight: bold;">已签到</font>' : ($value['status'] == 2 ? '<font style="color:#b2b2b2;">已取消</font>' : '<font style="color:#fabf2d;">未签到</font>'); 
			$data[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
		}

		return $data;
	}


	//获取教练
	public function coach_data($coach_id)
	{
		$this->db->select('nickname');
		$this->db->from('nmp_coach');
		$this->db->where('id',$coach_id);
		$query = $this->db->get();
		$data = $query->row_array();

		return $data['nickname'];

	}


	//更新教练
	public function updata_coach_data($arr=array(), $id)
	{
		if ($this->db->update($this->table,$arr,array('id'=>$id))){			
			return true;
		}else{
			return false;
		}

	}


	//更新状态
	public function updata_appoint_status($arr=array(), $id)
	{
		if ($this->db->update($this->table,$arr,array('id'=>$id))){			
			return true;
		}else{
			return false;
		}

	}


}





?>